package per.chenyl.framework.persistence.wrapper;


import per.chenyl.framework.persistence.common.util.CommonUtil;

import java.util.Objects;

public class BatchDeleteWrapper<T> extends AbstractWrapper<T> {
    /**
     * 删除列名称
     */
    private String columu;


    /**
     * 批量删除的值
     */
    private Object[] batchDeleteValues;
    /**
     * 批量删除SQL
     */
    private String batchSql;


    /**
     * @param columu 删除根据的列
     * @param values 删除根据的列的值
     */
    public BatchDeleteWrapper(String columu, Object[] values) {
        Objects.requireNonNull(columu);
        CommonUtil.requireNonEmptyArray(values);
        this.columu = columu;
        this.batchDeleteValues = values;
        delete();
    }

    public BatchDeleteWrapper(T entity, String columu, Object[] values) {
        super(entity);
        Objects.requireNonNull(columu);
        CommonUtil.requireNonEmptyArray(values);
        this.columu = columu;
        this.batchDeleteValues = values;
        delete();
    }

    private void delete() {
        StringBuilder s = new StringBuilder();
        s.append(" and ").append(columu).append(" in(");
        for (int i = 0; i < batchDeleteValues.length; i++) {
            s.append("?,");
        }
        String sql = s.substring(0, s.length() - 1);
        sql += ")";
        this.batchSql = sql;
    }


    @Override
    public Object[] getBatchDeleteValues() {
        return batchDeleteValues;
    }

    @Override
    public String getBatchSql() {
        return batchSql;
    }
}
